﻿var MobileCom = new Object();
MobileCom.getQueryString = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(decodeURI(r[2])); return null;
}

MobileCom.AlertMsg = function (content) {
    layer.open({
        content: content,
        btn: ['确认']
    });
}
MobileCom.getnowdate = function (format) {
    var now = new Date();

    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日
    var clock = year + "-";

    if (format == "yyyy-mm") {
        if (month < 10)
            clock += "0";
        clock += month + "-";
    }

    if (format == "yyyy-mm-dd") {
        if (month < 10)
            clock += "0";
        clock += month + "-";
        if (day < 10) {
            clock += "0";
        }
        clock += day + "-";
    }
    return (clock.substr(0, clock.length - 1));

}

MobileCom.getData = {
    current_page: 1,
    max_page: 0,
    init: function ($this) {
        this.bindscroll($this);
        this.show($this);
    },
    bindscroll: function ($this) {
        if ($this.length <= 0) { return false; }
        var self = this;
        $(window).scroll(function () {
            if ($("#loading").is(":visible"))
                return false;
            if (($(window).height() + $(window).scrollTop()) >= $(document).height()) {
                if (self.max_page > 0 && self.current_page >= self.max_page)
                    return false;
                self.current_page += 1;
                $("#loading").show();
                setTimeout(function () {
                    self.show($this);
                    //商品列表样式调整
                    $(".mall_pcp:odd").css({ "margin-right": "-10px", "float": "right" });
                    $(".mall_pcp img").width($("#cbox").width() * 0.5 - 27).height($("#cbox").width() * 0.5 - 27);
                    //location.href = "Index.aspx";
                }, 1000);
                //每页只加载一次，避免重复加载同一页
            }
        });
    },
    show: function ($this, data) {
        if ($this.length <= 0) { return false; }
        var self = this;
        var content = $this.find("div.content");
        if (self.current_page <= 1) {
            content.empty();
        }
        var d = { Page: self.current_page, mode: "getdata" };
        if (data)
            d = $.extend(d, data);

        $.ajax({
            type: "POST",
            url: location.href,
            data: d,
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.ok == 1) {

                    var datalist = $.parseJSON(data.data);
                    if (datalist.length > 0) {
                        self.max_page = data.maxPage;
                        var html = $("#templist").html();
                        $(datalist).each(function (i, v) {
                            content.append(html.substitute(v));
                        });
                    }
                    $("#loading").hide();
                    if (content.html() == "")
                        content.html('<div style="text-align:center; border:#ccc 1px solid; margin:6px auto; background-color:#EEE;">暂无信息</div>');
                }
                else {
                    alert(data.msg);
                }
            },
            error: function () {
                $("#loading").html("加载失败！").show();
            }
        });
        return false;
    }
}
//写cookies
MobileCom.setCookie = function (c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

//读取cookies
MobileCom.getCookie = function (name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

    if (arr = document.cookie.match(reg))

        return (arr[2]);
    else
        return null;
}
//删除cookies
MobileCom.delCookie = function (name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

